package com.share.finance.risk.core.web;

import com.alibaba.fastjson.JSONObject;
import com.share.common.common.Page;
import com.share.common.common.Result;
import com.share.common.utils.DbUtil;
import com.share.finance.risk.core.model.db.VariateConf;
import com.share.finance.risk.core.model.vo.ResultPageReq;
import com.share.finance.risk.core.model.vo.VariablePageReq;
import com.share.finance.risk.core.service.IProductVariateService;
import com.share.finance.risk.core.service.IResultInfoService;
import com.share.finance.risk.core.service.IVariateConfService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
public class ResultInfoController {
    @Autowired
    private IResultInfoService iResultInfoService;

    @Autowired
    private IVariateConfService variateConfService;

    @Autowired
    private IProductVariateService productVariateService;

    @Autowired
    private DataSource dataSource;

    @ApiOperation("清理删除表中的数据,保留最近的n条")
    @ResponseBody
    @RequestMapping(value = "/common/deleteLog")
    public Result deleteLog(Integer retainNum) throws SQLException {
        final String[] tables = {"cor_result_info"};
        Map<String, Object> reMap = DbUtil.deleteDataById(dataSource, tables, retainNum);
        return Result.success(reMap);
    }

    //分页查询
    @ResponseBody
    @RequestMapping(value = "/result/pageList")
    public Result resultPageList(ResultPageReq req) {
        Page page = iResultInfoService.pageList(req);
        return Result.success(page);
    }

    //刷新
    @RequestMapping(value = "/reload/conf")
    public Result reloadConf() {
        Object init1 = variateConfService.init();
        Object init = productVariateService.init();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("variateConfService", init1);
        jsonObject.put("productVariateService", init);
        return Result.success(jsonObject);
    }

    //分页查询
    @ResponseBody
    @RequestMapping(value = "/variable/pageList")
    public Result variablePageList(VariablePageReq req) {
        Page page = variateConfService.pageList(req);
        return Result.success(page);
    }

    @ApiOperation("查询所有的遍历配置")
    @ResponseBody
    @RequestMapping(value = "/variable/listAll")
    public Result listAllVariable() {
        return Result.success(variateConfService.listAllVariable());
    }

    @ApiOperation("查询 产品关联的所有变量配置")
    @ResponseBody
    @RequestMapping(value = "/productVariate/list")
    public Result productVariateList(String productCode, String eventType) {
        List<VariateConf> list = productVariateService.listVariateConfByProductCodeAndEventType(productCode, eventType);
        return Result.success(list);
    }
}
